<?php
/**
 * 客户端接口请求类
 *
 * 请妥善保存app_key和secret_key, 以免数据泄露
 *
 * @param string $app_key
 * @param string $secret_key
 */
namespace AuctionAppApi\Service;

use AuctionCore\System\AuctionException;
use Common\Lib\Alipay\AopClient;
use Common\Lib\Alipay\Request\AlipayTradeAppPayRequest;
use Common\Lib\Alipay\Request\AlipayTradeQueryRequest;

class AlipayService
{
	/**
	 * 阿里支付网关
	 */
	const url = "https://openapi.alipay.com/gateway.do";
	/**
	 * 商户id
	 */
	const appId = "2016092601977899";
	/**
	 * 私钥
	 */
	const privateKey ="MIICWwIBAAKBgQDAagsdyi4+4zfBlZnwM/yKHAPqS0niMfU8lWxn6JypLKzFU2vxDQSA7+uLc3Ettf6Qtw+fpx65rYq3bEIBc16tzVXaKNg1kMJUzQH4o/pHbsSrMCWklsKIH4E9DclCpcNWPniSD6BIDRDxo0TuoB5APHt9LiYLPMjQ4pJ47w/dSQIDAQABAoGAfHSa2BvsKbUiXU58kYeus05HogYstegGbbdgtR0aJ7rDzaaVzQbUgOmuezaiYCS1kbk2m9em4gbEELwvPWz4MWdwytCG+MaqCHSN8n34Dc76W9ECNa/plB4NE/fBlYsE24/8/GPz8hmaWOaiXM24xw4SzXWny/rUTAWNKHgprgECQQDqhFUpHj5/cMdTJn+LO++BP1Wn/7q328ye/EOklDPlDiV/GGncaEgP4Cv3erjMHd7MncoU9eM24x57tGmtAmQhAkEA0gpbyrQyJRkUaVTtIjKTEbHD9KXCJPXB91a21Im07DMLAoDsWKjV1qJBy0U7OgR7iHhs+yExiXb4Q4TltnNUKQJAND6W2joqoil1vkhtUY9go+KAKpUF+7+zrhd02k1J4lVsdQvzSW7R3veJ0/A7Cv38jIj6ZAEDRU8Dk3t1r11BAQJAdBahkTFzHan07BqyTdpiCr+TrJ4I5eCm6guVVXce9gVoKmQTPRVjyxM3BiAnkfN0S4MzyMEZ3f5RTRAg3mxzsQJAM3BOYiASe9OZGSJc/ImHO7wk4R78Q0M4iJMK9oU+KivzdZr6SgNQ548+NlWLgQqOm/T8njyn44FyNrAZnR7XKQ==";
	/**
	 * 数据类型
	 */
	const format     = "json";
	/**
	 * 字节码格式
	 */
	const charset    = "UTF-8";
	/**
	 * 阿里验签公钥
	 */
	const alipayPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB';
	/**
	 *
	 */
	//const NotifyUrl = 'http://testappapi.lpaiche.com/Recharge/notifyAlipay';
	const NotifyUrl = 'http://appapi.lpaiche.com/Recharge/notifyAlipay';
	/**
	 * 合作者ID
	 */
	const pid = 2088421519087564;
	/**
	 * @var 预定义变量
	 */
	protected   $m;




	public function __construct($alipay_config){

	}

	/**生成支付宝签名
	 * @param $order_no
	 * @param $amount
	 * @param $seller_id
	 * @param $subject
	 * @param $body
	 */
	public static function apppay($order_no,$amount,$alipay_user,$subject,$body){
		require_once(ROOT_DIR."/App/Common/Lib/Alipay/Request/AlipayTradeAppPayRequest.php");
		require_once(ROOT_DIR."/App/Common/Lib/Alipay/AopClient.php");
		$parameter = array(
				"out_trade_no"	=> $order_no,
				"subject"	=> $subject,
				"total_amount"	=> $amount,
				"body"	=> $body,
				"product_code"=>'QUICK_MSECURITY_PAY',
				//"seller_id"=>"2016101902241235",
				'timeout_express'=>'30m',
		);
		//获取前缀
		$route = $alipay_user['route'];
		//预定义支付宝参数
		$c = new AopClient();
		$c->appId = $alipay_user['appid'];
		$c->gatewayUrl = self::url;
		$c->format = self::format;
		$c->rsaPrivateKeyFilePath = '/Rsa/'.$route.'/app_private_key.pem';
		$request = new AlipayTradeAppPayRequest();
		$request->setApiVersion('1.0');
		$request->setNotifyUrl(self::NotifyUrl);
		$request->setBizContent(json_encode($parameter));
		$response= $c->getRequestInfo($request);
		//log 记录支付宝订单请求日志   以便日后订单查询
		info_log('Alipay',json_encode($parameter));
		return $response;
	}

	/**
	 * @param $out_trade_no
	 * @param $trade_no
	 * @return mixed
	 */
	public static function check_trade($out_trade_no,$trade_no,$alipay_user){
		require_once(ROOT_DIR."/App/Common/Lib/Alipay/Request/AlipayTradeQueryRequest.php");
		require_once(ROOT_DIR."/App/Common/Lib/Alipay/AopClient.php");
		//预定义支付宝参数
		$route = $alipay_user['route'];
		$aop = new AopClient ();
		$aop->appId = $alipay_user['appid'];
		$aop->gatewayUrl = self::url;
		$aop->format = self::format;
		$aop->rsaPrivateKeyFilePath = '/Rsa/'.$route.'/app_private_key.pem';
		$aop->postCharset='UTF-8';
		$query = new AlipayTradeQueryRequest();
		$query->setApiVersion('1.0');
		$query->setBizContent(json_encode(array(
				"out_trade_no"=>$out_trade_no,
				"trade_no"=>$trade_no
		)));
		$result = $aop ->getOrderStatus ( $query);
		info_log('Alipay/Check',$result);
		return json_decode($result,true);
	}


	public static function check_rsa(){

	}
}